package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAddress;
import com.qf.service.DtsAddressService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/address")
@RestController
@CrossOrigin
public class AdminAddressController {

    @Autowired
    private DtsAddressService dtsAddressService;

    /**
     * 查询收货地址列表
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     *
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"total": 16,
     * 		"items": [
     * 			{
     * 				"area": "隆回县",
     * 				"isDefault": false,
     * 				"areaId": 2019,
     * 				"address": "湖南省隆回县xxxxxx xxx",
     * 				"province": "湖南省",
     * 				"city": "邵阳市",
     * 				"name": "廖**",
     * 				"mobile": "15973******",
     * 				"id": 47,
     * 				"cityId": 219,
     * 				"userId": 43,
     * 				"provinceId": 18
     * 			},
     * 			{
     * 				"area": "温江区",
     * 				"isDefault": true,
     * 				"areaId": 2406,
     * 				"address": "洲际银海湾1xxxxxx xxx",
     * 				"province": "四川省",
     * 				"city": "成都市",
     * 				"name": "廖**",
     * 				"mobile": "13438******",
     * 				"id": 46,
     * 				"cityId": 271,
     * 				"userId": 76,
     * 				"provinceId": 23
     * 			}
     * 		]
     * 	},
     * 	"errmsg": "成功"
     * }
     */
    @GetMapping("list")
    @RequiresPermissions("admin:address:list")
    @RequiresPermissionsDesc(menu = "收货地址",button = "收货地址列表")
    public Object addressList(@RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "20")Integer limit,
                              Integer userId,String name,
                              String sort,String order){

        IPage<DtsAddress> iPage = dtsAddressService.selectList(page,limit,userId,name,sort,order);

        long total = iPage.getTotal();
        List<DtsAddress> records = iPage.getRecords();

        DtsAddress [] items = new DtsAddress[records.size()];
        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }
}
